﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [CountVIPepeatsInRepVatInv]
(
	@ViId int
)
RETURNS int
AS
BEGIN
	-- Declare the return variable here
	DECLARE @C int
	SET @C = 0
	
	SELECT @C = COUNT(*) FROM (
	SELECT vi.ID, pu.NAME, COUNT(pu.NAME)as COL FROM
		VAT_INVOICE as vi
		LEFT JOIN PRODUCT_UNIT as pu ON vi.ID = pu.VAT_INVOICE_ID
		WHERE vi.ID = @ViId
		GROUP BY vi.ID, pu.NAME
		) as t
	-- Return the result of the function
	IF @C = 0
	BEGIN
		SET @C = 1
	END
	RETURN @C

END